/**
 * <copyright> </copyright>
 * 
 * $Id$
 */
package org.dengues.model.database.impl;

import java.util.HashMap;
import org.dengues.model.database.*;
import java.util.Map;

import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBNote;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.DatabaseFactory;
import org.dengues.model.database.DatabasePackage;
import org.dengues.model.database.ForeignKey;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.impl.EFactoryImpl;
import org.eclipse.emf.ecore.plugin.EcorePlugin;

/**
 * <!-- begin-user-doc --> An implementation of the model <b>Factory</b>. <!-- end-user-doc -->
 * @generated
 */
public class DatabaseFactoryImpl extends EFactoryImpl implements DatabaseFactory {

    private static final String MAP_DELIMTER = ",;"; //$NON-NLS-1$

    /**
     * Creates the default factory implementation.
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public static DatabaseFactory init() {
        try {
            DatabaseFactory theDatabaseFactory = (DatabaseFactory)EPackage.Registry.INSTANCE.getEFactory("http:///org/dengues/model/database.ecore"); 
            if (theDatabaseFactory != null) {
                return theDatabaseFactory;
            }
        }
        catch (Exception exception) {
            EcorePlugin.INSTANCE.log(exception);
        }
        return new DatabaseFactoryImpl();
    }

    /**
     * Creates an instance of the factory.
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public DatabaseFactoryImpl() {
        super();
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    @Override
    public EObject create(EClass eClass) {
        switch (eClass.getClassifierID()) {
            case DatabasePackage.DB_TABLE: return createDBTable();
            case DatabasePackage.DB_COLUMN: return createDBColumn();
            case DatabasePackage.DATABASE_DIAGRAM: return createDatabaseDiagram();
            case DatabasePackage.FOREIGN_KEY: return createForeignKey();
            case DatabasePackage.DB_NOTE: return createDBNote();
            case DatabasePackage.SECTION_COLUMNS: return createSectionColumns();
            case DatabasePackage.SECTION_FILTERS: return createSectionFilters();
            case DatabasePackage.SECTION_GROUPS: return createSectionGroups();
            case DatabasePackage.SECTION_SORTS: return createSectionSorts();
            case DatabasePackage.SECTION_HAVINGS: return createSectionHavings();
            case DatabasePackage.SECTION_UNIONS: return createSectionUnions();
            case DatabasePackage.FOREIGN_KEY_LABEL: return createForeignKeyLabel();
            default:
                throw new IllegalArgumentException("The class '" + eClass.getName() + "' is not a valid classifier");
        }
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    @Override
    public Object createFromString(EDataType eDataType, String initialValue) {
        switch (eDataType.getClassifierID()) {
            case DatabasePackage.EMF_DATA_TYPE:
                return createEMFDataTypeFromString(eDataType, initialValue);
            case DatabasePackage.PROP_MAP:
                return createPropMapFromString(eDataType, initialValue);
            default:
                throw new IllegalArgumentException("The datatype '" + eDataType.getName() + "' is not a valid classifier");
        }
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    @Override
    public String convertToString(EDataType eDataType, Object instanceValue) {
        switch (eDataType.getClassifierID()) {
            case DatabasePackage.EMF_DATA_TYPE:
                return convertEMFDataTypeToString(eDataType, instanceValue);
            case DatabasePackage.PROP_MAP:
                return convertPropMapToString(eDataType, instanceValue);
            default:
                throw new IllegalArgumentException("The datatype '" + eDataType.getName() + "' is not a valid classifier");
        }
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public DBTable createDBTable() {
        DBTableImpl dbTable = new DBTableImpl();
        return dbTable;
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public DBColumn createDBColumn() {
        DBColumnImpl dbColumn = new DBColumnImpl();
        return dbColumn;
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public DatabaseDiagram createDatabaseDiagram() {
        DatabaseDiagramImpl databaseDiagram = new DatabaseDiagramImpl();
        return databaseDiagram;
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public ForeignKey createForeignKey() {
        ForeignKeyImpl foreignKey = new ForeignKeyImpl();
        return foreignKey;
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public DBNote createDBNote() {
        DBNoteImpl dbNote = new DBNoteImpl();
        return dbNote;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public SectionColumns createSectionColumns() {
        SectionColumnsImpl sectionColumns = new SectionColumnsImpl();
        return sectionColumns;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public SectionFilters createSectionFilters() {
        SectionFiltersImpl sectionFilters = new SectionFiltersImpl();
        return sectionFilters;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public SectionGroups createSectionGroups() {
        SectionGroupsImpl sectionGroups = new SectionGroupsImpl();
        return sectionGroups;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public SectionSorts createSectionSorts() {
        SectionSortsImpl sectionSorts = new SectionSortsImpl();
        return sectionSorts;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public SectionHavings createSectionHavings() {
        SectionHavingsImpl sectionHavings = new SectionHavingsImpl();
        return sectionHavings;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public SectionUnions createSectionUnions() {
        SectionUnionsImpl sectionUnions = new SectionUnionsImpl();
        return sectionUnions;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public ForeignKeyLabel createForeignKeyLabel() {
        ForeignKeyLabelImpl foreignKeyLabel = new ForeignKeyLabelImpl();
        return foreignKeyLabel;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public EMFDataType createEMFDataTypeFromString(EDataType eDataType, String initialValue) {
        EMFDataType result = EMFDataType.get(initialValue);
        if (result == null) throw new IllegalArgumentException("The value '" + initialValue + "' is not a valid enumerator of '" + eDataType.getName() + "'");
        return result;
    }

    /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public String convertEMFDataTypeToString(EDataType eDataType, Object instanceValue) {
        return instanceValue == null ? null : instanceValue.toString();
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     */
    public HashMap createPropMapFromString(EDataType eDataType, String initialValue) {
        HashMap<String, String> map = new HashMap<String, String>();
        if (initialValue == null || initialValue.length() == 0)
            return map;
        String[] tokens = initialValue.split(MAP_DELIMTER);

        if (tokens.length % 2 != 0) {
            String[] newTokens = new String[tokens.length + 1];
            int i = 0;
            for (; i < tokens.length; i++) {
                newTokens[i] = tokens[i];
            }
            newTokens[i] = "";
            tokens = newTokens;
        }
        int i = 0;
        while (i < tokens.length) {
            map.put(tokens[i++], tokens[i++]);
        }
        return map;
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     */
    public String convertPropMapToString(EDataType eDataType, Object instanceValue) {
        Map<String, String> map = (Map<String, String>) instanceValue;
        StringBuffer buffer = new StringBuffer(map.size() * 40);

        for (String key : map.keySet()) {
            String value = map.get(key);
            buffer.append(key).append(MAP_DELIMTER).append(value).append(MAP_DELIMTER);
        }
        return buffer.toString();
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @generated
     */
    public DatabasePackage getDatabasePackage() {
        return (DatabasePackage)getEPackage();
    }

    /**
     * <!-- begin-user-doc --> <!-- end-user-doc -->
     * @deprecated
     * @generated
     */
    @Deprecated
    public static DatabasePackage getPackage() {
        return DatabasePackage.eINSTANCE;
    }

} // DatabaseFactoryImpl
